package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.Exception.ErrorCode;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.mapper.RealAuthMapper;
import cn.wolfcode.p2p.base.query.QueryObject;
import cn.wolfcode.p2p.base.service.IRealAuthSevice;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.util.AssertUtil;
import cn.wolfcode.p2p.util.BitStatesUtils;
import cn.wolfcode.p2p.util.PageResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RealAuthSeviceImpl implements IRealAuthSevice {

    @Autowired
    private RealAuthMapper realAuthMapper;
    @Autowired
    private IUserInfoService userInfoService;

    /**
     * 保存实名认证申请对象
     * @param realAuth
     */
    @Override
    public void apply(RealAuth realAuth) {
        LoginInfo applier = UserContext.getLoginInfo();
        //数据校验
        UserInfo userInfo = userInfoService.getUserInfo(applier.getId());

        //是否已经通过实名认证
        AssertUtil.instance().isFalse(userInfo.hasRealAuth(),ErrorCode.ALREADY_AUTHENTICATION_SUCCESS);
        //判断是否重复提交审核
        AssertUtil.instance().isFalse(userInfo.getRealAuthId()!= null, ErrorCode.REPEATED_SUBMIT_AUTH);
        //获取要保存的参数
        RealAuth ra = new RealAuth();
        ra.setRealName(realAuth.getRealName());
        ra.setSex(realAuth.getSex());
        ra.setBornDate(realAuth.getBornDate());
        ra.setIdNumber(realAuth.getIdNumber());
        ra.setAddress(realAuth.getAddress());
        ra.setImage1(realAuth.getImage1());
        ra.setImage2(realAuth.getImage2());
        ra.setApplyTime(new Date());
        ra.setApplier(applier);
        realAuthMapper.insert(ra);
        //将创建的认证申请对象绑定到用户基本信息对象
        userInfo.setRealAuthId(ra.getId());
        userInfoService.update(userInfo);

    }

    @Override
    public RealAuth getRealAuth(Long realAuthId) {
        return realAuthMapper.selectByPrimaryKey(realAuthId);
    }

    @Override
    public PageResult query(QueryObject qo) {
        int count = realAuthMapper.queryForCount(qo);
        if (count == 0) {
            return PageResult.empty(qo.getPageSize());
        }
        int totalPage = count % qo.getPageSize() == 0 ? count / qo.getPageSize() : count / qo.getPageSize() + 1;
        if(totalPage < qo.getCurrentPage()){
            qo.setCurrentPage(totalPage);
        }
        List<RealAuth> listData = realAuthMapper.queryForList(qo);
        return new PageResult(listData,count,qo.getCurrentPage(),qo.getPageSize());
    }

    @Override
    public void audit(Long id, Integer state, String remark) {
        //对参数进行校验
        LoginInfo auditor = UserContext.getLoginInfo();
        RealAuth realAuth = realAuthMapper.selectByPrimaryKey(id);
        UserInfo userInfo = userInfoService.getUserInfo(realAuth.getApplier().getId());
        //是否处于待审核状态,否则抛出异常
        AssertUtil.instance().isFalse(realAuth.getState() != RealAuth.AUTH_WAIT,ErrorCode.HAVE_TO_REVIEW);
        //设置审核信息
        realAuth.setAuditTime(new Date());
        realAuth.setAuditor(auditor);
        realAuth.setRemark(remark);
        realAuth.setState(state);
        update(realAuth);
        //根据审核结果
        if(state == RealAuth.AUTH_SUCCESS){
            //审核通过,将申请人userInfo的位状态加上实名认证的位状态,将认证对象的状态设置为审核通过,将真实姓名与身份证号更新到userInfo
            userInfo.addBitState(BitStatesUtils.OP_REAL_AUTH);
            userInfo.setRealName(realAuth.getRealName());
            userInfo.setIdNumber(realAuth.getIdNumber());
        }
        //审核不通过,将认证对象的状态设置为审核失败,将申请人的userInfo的realAuthId更新为null,
        if(state == RealAuth.AUTH_REFUSE){
            userInfo.setRealAuthId(null);
        }
        userInfoService.update(userInfo);

    }
    @Override
    public void update(RealAuth realAuth){

        realAuthMapper.updateByPrimaryKey(realAuth);
    }

}
